<template>
  <div id="myCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <section class="c-sort-box">
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-show="data.total == 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <a class="c-666 fsize14 ml10 vam" href="/course"
              >这里空空如也，点击去寻找课程学习吧...</a
            >
          </section>
          <!-- /无数据提示 结束-->
          <article v-show="data.total > 0" class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="item in data.items" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img
                      :src="item.cover"
                      class="img-responsive"
                      :alt="item.title"
                    />
                    <div class="cc-mask">
                      <a
                        :href="'/course/' + item.courseId"
                        title="开始学习"
                        class="comm-btn c-btn-1"
                        >开始学习</a
                      >
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a
                      :href="'/course/' + item.courseId"
                      :title="item.title"
                      class="course-title fsize18 c-333"
                      >{{ item.title }}</a
                    >
                    <el-button
                      size="mini"
                      type="danger"
                      icon="el-icon-delete"
                      circle
                      style="float: right"
                      @click="removeDataById(item.courseId)"
                    ></el-button>
                  </h3>

                  <section class="mt10 hLh20 of">
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{ item.choiceCount }}人学习</i>
                      |
                      <i class="c-999 f-fA">{{ item.commentCount }}评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div v-show="data.total > 0">
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <a
              :class="{ undisable: !data.hasPrevious }"
              href="#"
              title="首页"
              @click.prevent="gotoPage(1)"
              >首</a
            >
            <a
              :class="{ undisable: !data.hasPrevious }"
              href="#"
              title="前一页"
              @click.prevent="gotoPage(data.current - 1)"
              >&lt;</a
            >
            <a
              v-for="page in data.pages"
              :key="page"
              :class="{
                current: data.current == page,
                undisable: data.current == page,
              }"
              :title="'第' + page + '页'"
              href="#"
              @click.prevent="gotoPage(page)"
              >{{ page }}</a
            >
            <a
              :class="{ undisable: !data.hasNext }"
              href="#"
              title="后一页"
              @click.prevent="gotoPage(data.current + 1)"
              >&gt;</a
            >
            <a
              :class="{ undisable: !data.hasNext }"
              href="#"
              title="末页"
              @click.prevent="gotoPage(data.pages)"
              >末</a
            >
            <div class="clear" />
          </div>
        </div>
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
import myCouresApi from "@/api/mycourse";

export default {
  asyncData({ params, error }) {
    return {
      ucenterId: params.id,
    };
  },
  data() {
    return {
      page: 1, //当前页
      limit: 8,
      data: {}, //课程列表
    };
  },
  created() {
    this.initMyCourse();
  },
  methods: {
    initMyCourse() {
      myCouresApi
        .getMyCourseList(this.page, this.limit, this.ucenterId)
        .then((response) => {
          this.data = response.data.data;
          console.log(this.data);
        });
    },

    //分页切换的方法
    gotoPage(page) {
      myCouresApi
        .getMyCourseList(page, this.limit, this.ucenterId)
        .then((response) => {
          this.data = response.data.data;
        });
    },

    toCoursePage() {
      this.$router.push({ path: "/course" });
    },

    removeDataById(id) {
      this.$confirm("此操作将删除该收藏课程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //点击确定，执行then方法
        //调用删除的方法
        myCouresApi.deletdMyCourese(id).then((response) => {
          if (response.data.success) {
            this.$message.success("删除成功");
            //回到列表页面
            this.initMyCourse();
          }
        });
      }); //点击取消，执行catch方法
    },
  },
};
</script>
<style scoped>
li.active a {
  background: #409eff;
  border-radius: 10px;
  color: #fff;
}
li.active a:hover {
  background: #409eff;
  border-radius: 10px;
  color: #fff;
}
li a:hover {
  color: #409eff;
}
li.current.bg-orange a:hover {
  color: white;
}
.hide {
  display: none;
}
.show {
  display: block;
}
.img-responsive {
  width: 100%;
}
</style>
